<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Credentials\Providers;

class EcsRamRoleProvider extends Provider
{
    protected $expirationSlot = 10;
    private $uri = "http://100.100.100.200/latest/meta-data/ram/security-credentials/";
    public function get()
    {
        $result = $this->getCredentialsInCache();
        if($result === NULL) {
            $result = $this->request();
            if(!(isset($result["AccessKeyId"]) && isset($result["AccessKeySecret"]) && isset($result["SecurityToken"]))) {
                throw new \AlibabaCloud\Client\Exception\ServerException($result, $this->error, \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
            }
            $this->cache($result->toArray());
        }
        return new \AlibabaCloud\Client\Credentials\StsCredential($result["AccessKeyId"], $result["AccessKeySecret"], $result["SecurityToken"]);
    }
    public function request()
    {
        $result = $this->getResponse();
        if($result->getStatusCode() === 404) {
            $message = "The role was not found in the instance";
            throw new \AlibabaCloud\Client\Exception\ClientException($message, \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
        }
        if(!$result->isSuccess()) {
            $message = "Error retrieving credentials from result";
            throw new \AlibabaCloud\Client\Exception\ServerException($result, $message, \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
        }
        return $result;
    }
    public function getResponse()
    {
        $credential = $this->client->getCredential();
        $url = $this->uri . $credential->getRoleName();
        $options = ["http_errors" => false, "timeout" => 1, "connect_timeout" => 1, "debug" => $this->client->isDebug()];
        try {
            return \AlibabaCloud\Client\Request\RpcRequest::createClient()->request("GET", $url, $options);
        } catch (\GuzzleHttp\Exception\GuzzleException $exception) {
            if(\Stringy\Stringy::create($exception->getMessage())->contains("timed")) {
                $message = "Timeout or instance does not belong to Alibaba Cloud";
            } else {
                $message = $exception->getMessage();
            }
            throw new \AlibabaCloud\Client\Exception\ClientException($message, \AlibabaCloud\Client\SDK::SERVER_UNREACHABLE, $exception);
        }
    }
}

?>